package com.diuse.outer.util.helper;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/10/25 0025.
 */
public class JsonHelp {

    private static Logger logger = Logger.getLogger(JsonHelp.class);
    private static Gson sGson = new Gson();

    public static String jsonFromObject(Object object) {
        return sGson.toJson(object);
    }

    public static <T> T objectFromJson(String json, Class<T> clz) {
        return sGson.fromJson(json, clz);
    }

    public static<T> T objectFromJson(InputStream in, Class<T> clz) {
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            T object = sGson.fromJson(reader, clz);
            reader.close();
            return object;
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(),e.getCause());
        } catch (IOException e) {
            logger.error(e.getMessage(), e.getCause());
        }

        return null;
    }






    public static void main(String args[]) {

    }
}
